package com.magicsoft.share.presenter.share;

import com.magicsoft.share.base.BasePresenter;
import com.magicsoft.share.bean.my.ArticleList;
import com.magicsoft.share.bean.share.OtherUserDetail;
import com.magicsoft.share.constant.APIService;
import com.magicsoft.share.constant.SPKey;
import com.magicsoft.share.rx_retrofit_network.location.retrofit.RetrofitUtil;
import com.magicsoft.share.rx_retrofit_network.location.rxandroid.LoadingTransformer;
import com.magicsoft.share.rx_retrofit_network.location.rxandroid.NetWorkTransformer;
import com.magicsoft.share.rx_retrofit_network.location.rxandroid.SimpleObserver;
import com.magicsoft.share.utils.SPUtils;
import com.magicsoft.share.view.share.IHomePageView;

import io.reactivex.annotations.NonNull;

/**
 * Created by lyf on 2017/11/29/029.
 */

public class HomePagePresenter extends BasePresenter<IHomePageView> {

    public void seeUserDetail(long otherId) {
        RetrofitUtil.getApi(APIService.class).
                seeUserDetail(SPUtils.getInstance().getLong(SPKey.ID), otherId)
                .compose(new NetWorkTransformer<OtherUserDetail>())
                .compose(new LoadingTransformer<OtherUserDetail>(mContext))
                .subscribe(new SimpleObserver<OtherUserDetail>(mCompositeDisposable) {
                    @Override
                    public void call(@NonNull OtherUserDetail otherUserDetail) {

                        getView().seeUserDetailSuccess(otherUserDetail);
                    }
                });

    }

    public void articleList(long otherId, int pageNum,
                            int pageSize) {
        RetrofitUtil.getApi(APIService.class)
                .articleList(SPUtils.getInstance().getLong(SPKey.ID), null, pageNum, pageSize, null, null, null, otherId)
                .compose(new NetWorkTransformer<ArticleList>())
                .compose(new LoadingTransformer<ArticleList>(mContext))
                .subscribe(new SimpleObserver<ArticleList>(mCompositeDisposable) {
                    @Override
                    public void call(@NonNull ArticleList articleList) {
                        if (null != getView()) {
                            getView().ArticleListSuccess(articleList);
                        }

                    }

                    @Override
                    public void showErrorMsg(Throwable e, String errorMsg) {
                        super.showErrorMsg(e, errorMsg);
                        if (null != getView()) {
                            getView().ArticleListFail();
                        }

                    }
                });
    }

    public void addFollow(long otherId) {
        RetrofitUtil.getApi(APIService.class).addFollow(SPUtils.getInstance().getString(SPKey.TOKEN), SPUtils.getInstance().getLong(SPKey.ID)
                , otherId)
                .compose(new NetWorkTransformer())
                .compose(new LoadingTransformer(mContext))
                .subscribe(new SimpleObserver(mCompositeDisposable) {
                    @Override
                    public void call(Object o) {

                        getView().followChangeSuccess();
                    }
                });
    }

    public void deleteFollow(long otherId) {
        RetrofitUtil.getApi(APIService.class).deleteFollow(SPUtils.getInstance().getString(SPKey.TOKEN), SPUtils.getInstance().getLong(SPKey.ID), otherId)
                .compose(new NetWorkTransformer())
                .compose(new LoadingTransformer(mContext))
                .subscribe(new SimpleObserver(mCompositeDisposable) {
                    @Override
                    public void call(Object o) {

                        getView().followChangeSuccess();
                    }
                });
    }
}
